package com.taotao.config;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
// mybaits dao 搜索路径
@MapperScan("com.taotao.dao")
public class MybatisSourceConfig {

	@Autowired
	private DataSourceProperties dataSourceProperties;
	// mybaits mapper xml搜索路径
	private final static String mapperLocations = "classpath:com/taotao/dao/*.xml";
	private org.apache.tomcat.jdbc.pool.DataSource datasource;
	// mybatis配置文件起始路径
	private final static String configLocation = "classpath:com/taotao/config/SqlMapConfig.xml";

	// 本来是阿里连接池 后来出了 问题就换成apache的连接池了
	@Bean(destroyMethod = "close")
	public DataSource dataSource() {
		System.out.println("注入jdbc DataSource！！！");
		DataSourceProperties config = dataSourceProperties;
		datasource = new org.apache.tomcat.jdbc.pool.DataSource();
		datasource.setUrl(config.getUrl());
		if (config.getUsername() != null) {
			datasource.setUsername(config.getUsername());
		}
		if (config.getPassword() != null) {
			datasource.setPassword(config.getPassword());
		}
		datasource.setDriverClassName(config.getDriver_class_namel());
		// datasource.setDbType(config.getType());
		datasource.setMaxActive(config.getMaxActive());
		datasource.setInitialSize(config.getInitialSize());
		datasource.setMinIdle(config.getMinIdle());
		// datasource.setMaxWait(config.getMaxWait());
		// datasource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
		// datasource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
		datasource.setTestWhileIdle(config.isTestWhileIdle());
		datasource.setTestOnBorrow(config.isTestOnBorrow());
		datasource.setTestOnReturn(config.isTestOnReturn());
		// datasource.setPoolPreparedStatements(config.isPoolPreparedStatements());
		// datasource.setMaxWait(config.getMaxWait());
		// try {
		// datasource.setFilters("stat,wall");
		// } catch (SQLException e) {
		// e.printStackTrace();
		// }
		return datasource;
	}

	/**
	 * 根据数据源创建SqlSessionFactory
	 */
	@Bean
	public SqlSessionFactory sqlSessionFactoryBean(DataSource ds) throws Exception {
		System.out.println("注入mybatis SqlSessionFactory！！！");
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(ds);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));
		sqlSessionFactoryBean.setConfigLocation(resolver.getResource(configLocation));
		sqlSessionFactoryBean.setTypeAliasesPackage("com.taotao.domain");;
		SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
		return sqlSessionFactory;
	}

	@Bean
	public PlatformTransactionManager transactionManager(DataSource ds) {
		return new DataSourceTransactionManager(ds);
	}
}
